package main.find;

public class BinnaryFind extends BaseFind {
    public BinnaryFind(int[] array) {
        super(array);
        this.name = "二分查找";
    }

    @Override
    public int find(int value) {
        return find(array, value, 0, array.length - 1);
    }

    protected int find(int[] array, int value, int start, int end) {
        int mid = this.getMid(start, end);
        if (start > mid || mid > end) {
            return -1;
        }
        if (array[mid] > value) {
            return find(array, value, start, mid - 1);
        } else if (array[mid] < value) {
            return find(array, value, mid + 1, end);
        } else {
            return mid;
        }
    }

    public int getMid(int start, int end) {
        return (start + end) >> 1;
    }


}
